package git.soulbgm;

import git.soulbgm.pojo.NodeDiskFreeRecord;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 磁盘空闲统计
 *
 * @author SoulBGM
 * @date 2023-08-07
 */
public class DiskFreeStatistics {

    private List<NodeDiskFreeRecord> getList() {
        List<NodeDiskFreeRecord> list = new ArrayList<>(50);
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 10000L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 9100L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 8200L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 7300L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 6400L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 5500L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 4600L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 3700L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 2800L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 1900L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 1000L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 8600L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 7700L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 6800L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 5900L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 5000L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 4100L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 3200L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 2300L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 1400L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 9800L));
        list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 8900L));
        // list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 7600L));
        // list.add(new NodeDiskFreeRecord(10, "node1", "10.1.1.1", new Date(), 5300L));

        list.add(new NodeDiskFreeRecord(11, "node2", "10.1.1.1", new Date(), 13000L));
        list.add(new NodeDiskFreeRecord(11, "node2", "10.1.1.1", new Date(), 8400L));
        list.add(new NodeDiskFreeRecord(11, "node2", "10.1.1.1", new Date(), 7100L));
        list.add(new NodeDiskFreeRecord(11, "node2", "10.1.1.1", new Date(), 6600L));
        list.add(new NodeDiskFreeRecord(11, "node2", "10.1.1.1", new Date(), 5400L));
        return list;
    }

    @Test
    public void test() {
        List<NodeDiskFreeRecord> list = getList();
        Map<Integer, List<NodeDiskFreeRecord>> map = list.stream().collect(Collectors.groupingBy(NodeDiskFreeRecord::getNodeCode));

        map.forEach((k, v) -> {
            List<Long> diffList = new ArrayList<>();
            int size = v.size();
            for (int i = 1; i < size; i++) {
                NodeDiskFreeRecord previous = v.get(i - 1);
                NodeDiskFreeRecord current = v.get(i);
                long diff = previous.getDiskFree() - current.getDiskFree();
                if (diff < 0) {
                    continue;
                }
                diffList.add(diff);
            }
            double avgDiff = diffList.stream().mapToLong(o -> o).average().orElse(0);
            NodeDiskFreeRecord last = v.get(size - 1);
            System.out.println(avgDiff);
            System.out.println(last.getDiskFree() / avgDiff);
        });
    }

}
